import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.hre.db.dao.LeaveDAO;
import com.google.gson.JsonObject;

/**
 * Servlet implementation class LeaveInsertServlet
 */
public class LeaveInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final SimpleDateFormat dateFormatMinute = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final SimpleDateFormat hourFormat = new SimpleDateFormat(
			"HH");

	/**
	 * @see HttpServlet#HttpServlet()
	 */

	public LeaveInsertServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int userId = Integer.parseInt(request.getParameter("userId"));
		int supervisorId = Integer.parseInt(request.getParameter("supervisor"));
		int leaveTypeId = Integer.parseInt(request.getParameter("leaveType"));
		String empComment = request.getParameter("empComment");
		
		String leaveFromStr = request.getParameter("firstLeave").split("T")[0];
		leaveFromStr += " " + request.getParameter("firstLeaveTime");
		String leaveToStr = request.getParameter("lastLeave").split("T")[0];
		leaveToStr += " " + request.getParameter("lastLeaveTime");
		Date leaveFrom = null;
		try {
			leaveFrom = dateFormatMinute.parse(leaveFromStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date leaveTo = null;
		try {
			leaveTo = dateFormatMinute.parse(leaveToStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JsonObject myObj = new JsonObject();
		try {

			if (leaveFrom.compareTo(leaveTo) >= 0) {
				myObj.addProperty("isValid", false);
				myObj.addProperty("msg",
						"Leave from date must be greater than leave to date!");

			}
			else if (leaveTypeId == 2 && computeLeaveDays(leaveFrom, leaveTo) > 12) {
				myObj.addProperty("isValid", false);
				myObj.addProperty("msg",
						"Can not create leave request with paid leave type any more!");
			}
			else {
				new LeaveDAO().InsertLeaveRequest(userId,supervisorId,leaveTypeId,leaveFromStr,leaveToStr,empComment);
				myObj.addProperty("isValid", true);

			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		myObj.addProperty("success", true);
		response.getWriter().println(myObj.toString());
		response.getWriter().close();

	}

	public static float computeLeaveDays(Date start, Date end)
			throws ParseException {
		Date d1 = dateFormat.parse(dateFormat.format(start));
		Date d2 = dateFormat.parse(dateFormat.format(end));
		float diff = (float) ((d2.getTime() - d1.getTime()) / (1000.0 * 60 * 60 * 24));
		int hourD1 = Integer.parseInt(hourFormat.format(start));
		if (hourD1 == 12) {
			diff -= 0.5;
		} else if (hourD1 > 12) {
			diff -= 1;
		}
		int hourD2 = Integer.parseInt(hourFormat.format(end));
		if (hourD2 == 12) {
			diff += 0.5;
		} else if (hourD2 > 12) {
			diff += 1;
		}
		return diff;
	}
}
